package org.codert.codertadmin.web.controller;

import lombok.Data;
import org.codert.codertadmin.domain.mapper.GoodsMapper;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * @author: TanJingyu
 * @create: 2021-08-05 10:06
 **/
@RestController
@RequestMapping("/hello")
public class HelloController {
//    @Autowired
//    private GoodsMapper goodsMapper;
//    @Autowired
//    private ApplicationContext context;


    /**
     * ObjectProvider可以以懒加载的形势创建bean。
     * 反省只是为了避免手动转型而已。
     * 注入哪个bean是根据名称来的
     */
    public HelloController(ObjectProvider<HttpMessageConverters> messageConvertersProvider, ObjectProvider<GoodsMapper> goodsMapper) {
        System.out.println("jjjjj");
    }

    @PostMapping("/demo")
    public Object demo(@RequestBody @Validated Demo demo) {
        return demo;
    }

    @GetMapping
    public String hello(String v) {
        return String.format("接收到的参数v: %s", v);
    }

    /**
     * 此时的HelloController是个lite模式的配置类
     * 其实HelloController已经被ComponentScan扫描进来认定为是Spring组件了，就说明它至少被@Component标注了，所以就保证了它至少是个lite模式的配置类
     * 如何成为lite模式的配置类？
     * 当某个类被认定为是 'Spring容器组件' 的时候，具有一下特性：
     * 加个@Bean注解。在类上加个@Import、@Component、@ComponentScan、@ImportResource
     */
    @Bean
    public Demo demo() {
        return new Demo();
    }

    @Data
    public static class Demo {
        @NotBlank
        private String name;
        private Integer age;
        @Valid
        private DemoSon son;
    }

    @Data
    public static class DemoSon {
        @NotBlank
        private String name;
    }
}
